计算机与现代化 ›› 2010, Vol. 1 ›› Issue (8): 120-123.doi: 10.3969/j.issn.1006-2475.2010.08.034

• 应用与开发 • 上一篇    下一篇

RTTI 机制浅析

何洪辉1,刘骥宇2   

  1. 1.河南大学计算中心,河南 开封475004; 2.商丘师范学院软件学院,河南 商丘 476000
  • 收稿日期:2010-04-06 修回日期:1900-01-01 出版日期:2010-08-27 发布日期:2010-08-27

Analysis of RTTI Realization

HE Hong-hui1, LIU Ji-yu2   

  1. 1.Computation Center, Henan University, Kaifeng 475004, China;2.School of Software, Shangqiu Normal College, Shangqiu 476000, China
  • Received:2010-04-06 Revised:1900-01-01 Online:2010-08-27 Published:2010-08-27

摘要: 编译结束后,由于C++编译器不再保留类的定义信息,致使程序在运行时刻(RunTime)无法再次获得对象的类信息,如类名、类的继承关系等,因此C++中的一些重要操作,如动态转类型(dynamic_cast)、对象的持久化(Persistence)等将无法实现,而RTTI正是解决这一问题的关键。鉴于RTTI长期不为大多数开发人员所理解,本文通过对一个简单RTTI的实现,探讨RTTI的实现机制。

关键词: RTTI, 动态类型转换, 对象持久化

Abstract: After compiling a source file by C++ compiler, the class definition information isn’t remained in object file. Therefore, the program at runtime can’t acquire the class information, such as class name,class hierarchy and so on, and consequently many operations such as dynamic_cast, persistence are impossible to be completed. RTTI is the key to conquer this problem. Although a great deal of importance is attached to RTTI, many C++ programmers are still confused by RTTI realization. In this paper, a concrete RTTI is realized, and it gives C++ programmers a chance to have a glimpse of RTTI realization. 

Key words: RTTI, dynamic_cast, persistence